﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Data;
using DB = HTComics.Data;
using System.Web.UI.HtmlControls;
using System.IO;
using HTComics.Web.Common;

namespace HTComics.Web.Order
{
    public partial class Cart : System.Web.UI.Page
    {
        protected StringBuilder tblCart = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateCart();
        }

        private void PopulateCart()
        {
            Dictionary<string, Dictionary<string, Dictionary<string, string>>> data = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(Server.UrlDecode(Request.Cookies["cart"].Value));            
            DataSet dsResult;
            double discount = DB.Misc.GetGlobalDiscount();
            double sellPrice, discountPrice;
            int qty;
            bool hasData = false;
            double tQty=0, tSellPrice=0, tDiscountPrice=0;

            tblCart.Append("<table width='100%' cellspacing='0'>");
            tblCart.Append("<tr>");
            tblCart.Append("<td class='isuDetHeading_L'>&nbsp;</td>");
            tblCart.Append("<td class='isuDetHeading_M'>Comic</td>");
            tblCart.Append("<td class='isuDetHeading_M'>Issue</td>");
            tblCart.Append("<td class='isuDetHeading_M'>Condition</td>");
            tblCart.Append("<td class='isuDetHeading_M'>Quantity</td>");

            if (discount > 0)
            {
                tblCart.Append("<td class='isuDetHeading_M'>Guide Price</td>");
                tblCart.Append("<td class='isuDetHeading_M'>Our Price</td>");
                tblCart.Append("<td class='isuDetHeading_M'>You Save</td>");
            }
            else
            {
                tblCart.Append("<td class='isuDetHeading_M'>Price</td>");
            }
            tblCart.Append("<td class='isuDetHeading_R'><button id='btnRemoveAll' title='Remove all items from Cart'>Remove All</button></td>");
            tblCart.Append("</tr>");

            foreach (string comicId in data.Keys)
            {
                foreach (string issue in data[comicId].Keys)
                {
                    foreach (string condition in data[comicId][issue].Keys)
                    {
                        qty = Convert.ToInt32(data[comicId][issue][condition]);
                        if (qty > 0)
                        {
                            dsResult = DB.Search.GetIssueDetails(comicId, issue, condition);

                            string imgLoc = dsResult.Tables[0].Rows[0][0].ToString()[0] + "/" + dsResult.Tables[0].Rows[0][0];
                            string img;

                            if (File.Exists(HttpContext.Current.Server.MapPath("~/Images/Covers/" + imgLoc + "/" + issue + ".jpg")))
                                img = "<a href='#'><img id=img" + issue + "' src=\"../Images/Covers/" + imgLoc + "/" + issue + ".jpg\" height='60px' width='45px' onclick=showDetails(this) border='0' cn=\"" + dsResult.Tables[0].Rows[0]["Title"] + "\" issue='" + issue + "' cp='" + dsResult.Tables[0].Rows[0]["CoverPrice"] + "' sl='" + dsResult.Tables[0].Rows[0]["Storylines"] + "' wrt='" + dsResult.Tables[0].Rows[0]["Writer"] + "' art='" + dsResult.Tables[0].Rows[0]["Artist"] + "' cd='" + Convert.ToDateTime(dsResult.Tables[0].Rows[0]["CoverDate"]).ToString("MMM yyyy") + "'/></a>";
                            else
                                img = "<img src='../Images/noimage.png' height='30px' width='22px' />";

                            sellPrice = Convert.ToDouble(dsResult.Tables[1].Rows[0]["SellingPrice"]);
                            sellPrice = Pricing.CalculatePrice(sellPrice, Convert.ToInt32(dsResult.Tables[1].Rows[0]["ConditionId"]));
                            
                            discountPrice = sellPrice - (sellPrice * discount / 100);

                            sellPrice *= qty;
                            discountPrice *= qty;

                            sellPrice = Math.Round(sellPrice, 2);
                            discountPrice = Math.Round(discountPrice, 2);

                            tblCart.Append("<tr>");
                            tblCart.Append("<td class='isuDetRow_L'>" + img + "</td>");
                            tblCart.Append("<td class='isuDetRow_M'>" + dsResult.Tables[0].Rows[0]["Title"] + "</td>");
                            tblCart.Append("<td class='isuDetRow_M'>" + issue + "</td>");
                            tblCart.Append("<td class='isuDetRow_M'>" + dsResult.Tables[1].Rows[0]["Name"] + "</td>");
                            tblCart.Append("<td class='isuDetRow_M'>" + data[comicId][issue][condition] + "</td>");

                            if (discount > 0)
                            {
                                tblCart.Append("<td class='isuDetRow_M'><del>€" + string.Format("{0:0.00}",sellPrice) + "</del></td>");
                                tblCart.Append("<td class='isuDetRow_M' style='font-weight:bold;'>€" + string.Format("{0:0.00}", discountPrice) + "</td>");
                                tblCart.Append("<td class='isuDetRow_M' style='color:red;'>€" + string.Format("{0:0.00}", (sellPrice - discountPrice)) + "</td>");
                            }
                            else
                            {
                                tblCart.Append("<td class='isuDetRow_M'>€" + string.Format("{0:0.00}", sellPrice) + "</td>");
                            }

                            tblCart.Append("<td class='isuDetRow_M'><button title='Remove Item From Cart' att='rem' cid='" + comicId + "' issue='" + issue + "' condId='" + condition + "'><img src='../Images/delCart.png' height='16px' width='16px' />" + "" + "</button></td>");
                            tblCart.Append("</tr>");

                            tQty += qty;
                            tSellPrice += sellPrice;
                            tDiscountPrice += discountPrice;

                            hasData = true;
                        }
                    }
                }
            }

            if (!hasData)
            {
                tblCart = new StringBuilder();
                tblCart.Append("There are no items in your cart!");
            }
            else //Total Row
            {
                tblCart.Append("<tr>");
                tblCart.Append("<td class='isuDetRow_L' colspan='4'>Total (inc. VAT)</td>");
                tblCart.Append("<td class='isuDetRow_M'>" + tQty + "</td>");

                if (discount > 0)
                {
                    tblCart.Append("<td class='isuDetRow_M'><del>€" + string.Format("{0:0.00}",tSellPrice) + "</del></td>");
                    tblCart.Append("<td class='isuDetRow_M' style='font-weight:bold;'>€" + string.Format("{0:0.00}", tDiscountPrice) + "</td>");
                    tblCart.Append("<td class='isuDetRow_M' style='color:red;'>€" + string.Format("{0:0.00}", (tSellPrice - tDiscountPrice)) + "</td>");
                }
                else
                {
                    tblCart.Append("<td class='isuDetRow_M'>€" + string.Format("{0:0.00}", tSellPrice) + "</td>");
                }
                tblCart.Append("<td class='isuDetHeading_R'><button id='btnOrder'>Complete Order</button></td>");
                tblCart.Append("</tr>");

                tblCart.Append("</table>");
            }
        }
    }
}